package com.hunttown.mes.convert;

import com.google.common.collect.Lists;
import com.hunttown.mes.common.utils.BeanUtils;
import com.hunttown.mes.domain.TransportUserCard;
import com.hunttown.mes.rpc.exception.ConvertException;
import com.hunttown.mes.rpc.domain.TransportUserCardDTO;
import com.hunttown.mes.rpc.domain.enums.ErrorCode;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Collections;
import java.util.List;

/**
 * created by wangjunfu on 2019-07-06 10:58:59
 */
public class TransportUserCardConverts {

    private static Logger LOG = LoggerFactory.getLogger(TransportUserCardConverts.class);

    public static List<TransportUserCardDTO> convert(List<TransportUserCard> transportUserCardList) {
        if (CollectionUtils.isEmpty(transportUserCardList)) {
            return Collections.EMPTY_LIST;
        }

        List<TransportUserCardDTO> transportUserCardListResult = Lists.newArrayList();
        for (TransportUserCard transportUserCard : transportUserCardList) {
            transportUserCardListResult.add(convert(transportUserCard));
        }
        return transportUserCardListResult;
    }

    public static TransportUserCardDTO convert(TransportUserCard transportUserCard) {
        try {
            return BeanUtils.copy(transportUserCard, TransportUserCardDTO.class);
        } catch (Exception e) {
            LOG.error("TransportUserCard到TransportUserCardDTO转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportUserCard到TransportUserCardDTO转换失败", e);
        }
    }

    public static TransportUserCard convert(TransportUserCardDTO transportUserCardDTO) {
        try {
            return BeanUtils.copy(transportUserCardDTO, TransportUserCard.class);
        } catch (Exception e) {
            LOG.error("TransportUserCardDTO到TransportUserCard转换失败!", e);
            throw new ConvertException(ErrorCode.CONVERT_ERROR, "TransportUserCardDTO到TransportUserCard转换失败", e);
        }
    }
}